<template>
  <!-- 新增、修改总工作项 -->
  <base-modal
    :title="initData.pageType === 'edit' ? '安全责任指标考核项目编辑' : '安全责任指标考核项目添加'"
    cancelText="取消"
    okText="保存"
    :width="800"
    v-on="$listeners"
    @ok="handleOk"
    @cancel="handleCancel"
    :confirmLoading="loading.saving"
  >
    <DynamicForm :loading="false" ref="dynamicForm" v-bind="$attrs" />
  </base-modal>
</template>

<script>
import { addExamContent, updateExamContent } from '@/api/safetySupervision/common.js'
import DynamicForm from '@/components/DynamicForm'
import BaseModal from '@/components/BaseModal'
export default {
    name:'a'+Date.now(),  components: {
    BaseModal,
    DynamicForm,
  },
  props: {
    initData: {
      type: Object,
      default() {
        return {}
      },
    },
    getColumnCode: {
      type: Function,
      default() {
        return () => {}
      }
    }
  },
  data() {
    return {
      loading: {
        saving: false
      },
    }
  },
  methods: {
    handleCancel() {
      this.$emit('cancel')
    },
    handleOk() {
      this.$emit('ok')
      const itemChildList = []
      const formRef = this.$refs.dynamicForm
      formRef.getFormRef().validate(async (valid) => {
        if (valid) {
          if(this.loading.saving) return
          this.loading.saving = true
          const form = formRef.getFormInfo()
          for (let key in form) {
            if (form.hasOwnProperty(key) && key !== 'id') {
              itemChildList.push({
                headerItemId: key,
                value: this.getColumnCode(key) === '3' ? Number(form[key]).toFixed(2) : form[key]
              })
            }
          }
          const fn = this.initData.pageType === 'edit' ? updateExamContent : addExamContent
          const [err, res] = await fn({
            formId: this.initData.formId,
            itemChildList,
            id: form.id || void 0,
          })
          if (!err) {
            this.$message.success('成功')
            this.$emit('confirm')
          }
          this.loading.saving = false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
form .ant-mentions,
form textarea.ant-input {
  width: 200px;
  height: auto !important;
}
form .ant-mentions,
form textarea.ant-input {
  width: 100%;
}
</style>